package com.chenxbook.modules.examples.eventlistener;

import org.apache.commons.collections4.CollectionUtils;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;

import java.util.ArrayList;
import java.util.List;

/**
 *  事件监听必须实现EventListener
 *  <br> 维护流程运行的日志，并保存在流程变量中
 * */
public class LogListener implements EventListener {
    private static final long serialVersionUID = 5512465449605278212L;
    String msg;
    @Override
    public void notify(EventListenerExecution execution) {
        //通过execution接口对象，我们可以获取所有流程变量
        List<String> logs = (List<String>) execution.getVariable("logs");
        if (CollectionUtils.isEmpty(logs)){
            logs = new ArrayList<String>();
            execution.setVariable("logs",logs);
        }
        logs.add(msg);
        execution.setVariable("logs",logs);
    }
}
